package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.BeanUtils;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.service.IInvestorGlInterfaceService;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.entity.PrTransactionCodeTable;
import com.jrx.anytxn.param.service.system.ITransactionCodeTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出资方总账流水接口实现类
 *
 * @author liuyang
 * @date 2020-05-20
 */
@Service
public class InvestorGlInterfaceServiceImpl implements IInvestorGlInterfaceService {

    private static final Logger logger = LoggerFactory.getLogger(InvestorGlInterfaceServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;

    @Autowired
    private ITransactionCodeTableService transactionCodeTableService;

    /**
     * 贷款时总账流水生成
     *
     * @param accountMainInvestorInfo 交易账户
     * @param glAmt 交易金额
     * @param txnCode 交易码
     * @param accountingPhase 会计状态
     * @param businessDate 业务日期
     * @return TlGlInterfaceInvestorInfo 总账流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-20
     */
    @Override
    public TlGlInterfaceInvestorInfo buildTlGlInterfaceInvestorInfo(AmAccountMainInvestorInfo accountMainInvestorInfo, BigDecimal glAmt, String txnCode, String accountingPhase, Date businessDate) throws TxnException {

        if (glAmt.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(txnCode,accountMainInvestorInfo.getTenantId());

        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(accountMainInvestorInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(businessDate); //业务日期	YYYY-MM-DD	当前业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(businessDate, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(accountMainInvestorInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(txnCode);//交易码 已有	消费信贷核算事件对应的交易码 具体值：按照消费信贷系统规范约定会计引擎模块会维护与消费信贷系统一致的数据字典
        tlGlInterfaceInfo.setAmount(glAmt);//交易金额	　	贷款订单表-贷款金额
        tlGlInterfaceInfo.setInvestorLoanId(accountMainInvestorInfo.getInvestorLoanId());// 出资方贷款id
        tlGlInterfaceInfo.setInvestorId(accountMainInvestorInfo.getInvestorId());// 出资方id
        tlGlInterfaceInfo.setLoanId(accountMainInvestorInfo.getLoanId());//贷款id	　	同贷款订单表
        tlGlInterfaceInfo.setLoanProductId(accountMainInvestorInfo.getProductId());//　	贷款产品id	　	同贷款订单表
        tlGlInterfaceInfo.setCustomerId(accountMainInvestorInfo.getCustomerId());//	客户号	　	同贷款订单表
        String txnDesc = transactionCodeTable.getChineseName();
        tlGlInterfaceInfo.setTxnDesc(txnDesc);//交易描述	业务行摘要说明，源系统可视情况自动生成  产品名称
        tlGlInterfaceInfo.setTxnAccountId(accountMainInvestorInfo.getAccountId());//交易账户号	　	贷款订单表-客户收款账号
        tlGlInterfaceInfo.setLoanClassify(accountMainInvestorInfo.getLoanClassify());//五级分类	 同贷款订单表
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(accountMainInvestorInfo.getChannel());//渠道	　	同贷款订单表
        tlGlInterfaceInfo.setCategory(accountMainInvestorInfo.getNonAccrualsFlag());
        tlGlInterfaceInfo.setTotalTenor(accountMainInvestorInfo.getTotalTenor());
        tlGlInterfaceInfo.setTotalDays(accountMainInvestorInfo.getTotalDays());
        tlGlInterfaceInfo.setAccountingPhase(accountingPhase);
        tlGlInterfaceInfo.setLiabilityFlag(accountMainInvestorInfo.getLiabilityFlag());
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号	　	初始值：
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorId(accountMainInvestorInfo.getInvestorId());
        tlGlInterfaceInfo.setInvestorLoanId(accountMainInvestorInfo.getInvestorLoanId());
        return tlGlInterfaceInfo;
    }

    /**
     * 通过09交易账户生成gl流水
     *
     * @param paymentLog
     * @return
     */
    private TlGlInterfaceInvestorInfo buildTlGlInterfaceInfo(TlPaymentAllocationInvestorLog paymentLog) throws TxnException {
        logger.info("通过09交易账户生成gl流水,paymentLog:{}", BeanUtils.toMap(paymentLog));

        if (paymentLog.getPostingAmount().compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(paymentLog.getTxnCode()
                ,paymentLog.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }
        TlGlInterfaceInvestorInfo gl = new TlGlInterfaceInvestorInfo();
        try {
            gl.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(TxnRespCode.ERROR.getUserTips());
        }
        gl.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));
        gl.setInvestorId(paymentLog.getInvestorId());
        gl.setInvestorLoanId(paymentLog.getInvestorLoanId());
        gl.setGlDate(paymentLog.getPostingDate());
        gl.setBatchId(DateUtils.format(paymentLog.getPostingDate(), DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);
        gl.setTenantId(paymentLog.getTenantId());
        gl.setAmount(paymentLog.getPostingAmount());
        gl.setLoanProductId(paymentLog.getProductId());
        gl.setTxnAccountId(paymentLog.getConnectAccountId());
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);
        gl.setOrganizationId(paymentLog.getOrganizationId());//　	机构号	　	同贷款订单表
        gl.setTxnCode(paymentLog.getTxnCode());//交易码
        gl.setLoanId(paymentLog.getLoanId());//贷款id
        gl.setCustomerId(paymentLog.getCustomerId());//客户号
        gl.setTxnAccountId(paymentLog.getAccountId());//交易账户号
        gl.setChannel(paymentLog.getChannel());
        gl.setLoanClassify(paymentLog.getLoanClassify());
        String txnDesc = transactionCodeTable.getChineseName();
        gl.setTxnDesc(txnDesc);
        gl.setCategory(paymentLog.getNonAccrualsFlag());
        gl.setTotalTenor(paymentLog.getTotalTenor());
        gl.setTotalDays(paymentLog.getTotalDays());
        gl.setAccountingPhase(paymentLog.getAccountingPhase());
        gl.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        gl.setLiabilityFlag(paymentLog.getLiabilityFlag());
        gl.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号
        gl.setUpdateBy(TransBizConstant.SYS_OPRATE);
        logger.info("通过09交易账户生成gl流水,gl:{}", BeanUtils.toMap(gl));
        return gl;
    }

    private TlGlInterfaceInvestorInfo buildTaxTlGlInterfaceInfo(TlPaymentAllocationInvestorLog paymentLog) throws TxnException {
        BigDecimal taxAmt = paymentLog.getTaxAmt();
        if(taxAmt.compareTo(BigDecimal.ZERO)<=0){
            return null;
        }

        String txnCode = "";
        if(TransBizConstant.ACCOUNT_TYPE_04.equals(paymentLog.getConnectAccountType())){
            txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_INTR_TAX;
        }else if(TransBizConstant.ACCOUNT_TYPE_11.equals(paymentLog.getConnectAccountType())){
            txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_PEN_INTR_TAX;
        }else if(TransBizConstant.ACCOUNT_TYPE_12.equals(paymentLog.getConnectAccountType())){
            txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_COMP_INTR_TAX;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(txnCode,paymentLog.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }
        TlGlInterfaceInvestorInfo gl = new TlGlInterfaceInvestorInfo();
        try {
            gl.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(TxnRespCode.ERROR.getUserTips());
        }
        gl.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));
        gl.setGlDate(paymentLog.getPostingDate());
        gl.setBatchId(DateUtils.format(paymentLog.getPostingDate(), DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);
        gl.setTenantId(paymentLog.getTenantId());
        gl.setAmount(paymentLog.getTaxAmt());
        gl.setInvestorId(paymentLog.getInvestorId());
        gl.setInvestorLoanId(paymentLog.getInvestorLoanId());
        gl.setLoanProductId(paymentLog.getProductId());
        gl.setTxnAccountId(paymentLog.getConnectAccountId());
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);
        gl.setOrganizationId(paymentLog.getOrganizationId());//　	机构号	　	同贷款订单表
        gl.setTxnCode(txnCode);//交易码
        gl.setLoanId(paymentLog.getLoanId());//贷款id
        gl.setCustomerId(paymentLog.getCustomerId());//客户号
        gl.setTxnAccountId(paymentLog.getAccountId());//交易账户号
        gl.setChannel(paymentLog.getChannel());
        gl.setLoanClassify(paymentLog.getLoanClassify());
        String txnDesc = transactionCodeTable.getChineseName();
        gl.setTxnDesc(txnDesc);
        gl.setCategory(paymentLog.getNonAccrualsFlag());
        gl.setTotalTenor(paymentLog.getTotalTenor());
        gl.setTotalDays(paymentLog.getTotalDays());
        gl.setAccountingPhase(paymentLog.getAccountingPhase());
        gl.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        gl.setLiabilityFlag(paymentLog.getLiabilityFlag());
        gl.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号
        gl.setUpdateBy(TransBizConstant.SYS_OPRATE);
        logger.info("通过09交易账户生成gl流水,gl:{}", BeanUtils.toMap(gl));
        return gl;
    }

    /**
     * 通过09交易账户生成gl流水
     *
     * @param paymentLogList
     * @return
     */
    @Override
    public List<TlGlInterfaceInvestorInfo> buildTlGlInterfaceInfoList(List<TlPaymentAllocationInvestorLog> paymentLogList) throws TxnException {
        List<TlGlInterfaceInvestorInfo> glInterfaceInfoList = new ArrayList<>();
        for (TlPaymentAllocationInvestorLog paymentLog : paymentLogList) {
            //生成还款gl流水
            TlGlInterfaceInvestorInfo gl = this.buildTlGlInterfaceInfo(paymentLog);
            if(null != gl) {
                glInterfaceInfoList.add(gl);
            }

            if(TransBizConstant.NON_ACCRUALS_FLA_1.equals(paymentLog.getNonAccrualsFlag())){
                //表外还款  生成表外转表内gl流水
                BigDecimal glAmt = paymentLog.getPostingAmount().subtract(paymentLog.getTaxAmt());
                String txnCode = "";
                switch (paymentLog.getConnectAccountType()){
                    //本金
                    case TransBizConstant.ACCOUNT_TYPE_06:
                        txnCode = TransBizConstant.TXN_CODE_PRIN_TRAN_INNON;
                        break;
                    //利息
                    case TransBizConstant.ACCOUNT_TYPE_04:
                        txnCode = TransBizConstant.TXN_CODE_INTR_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_INTR_TRAN_OUT_INNON;
                        }
                        break;
                    //罚息
                    case TransBizConstant.ACCOUNT_TYPE_11:
                        txnCode = TransBizConstant.TXN_CODE_PENT_INTR_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_PENT_INTR_TRAN_OUT_INNON;
                        }
                        break;
                    //复利
                    case TransBizConstant.ACCOUNT_TYPE_12:
                        txnCode = TransBizConstant.TXN_CODE_COMP_INTR_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_COMP_INTR_TRAN_OUT_INNON;
                        }
                        break;
                }
                if(StringUtils.isNotEmpty(txnCode)){
                    TlGlInterfaceInvestorInfo tranGl = this.buildTransferGlInterfaceInfo(paymentLog,glAmt,txnCode);
                    if (null != tranGl) {
                        glInterfaceInfoList.add(tranGl);
                    }
                }
            }
            //taxAmt税金大于0  需要处理税金   核销还款 或 表外还款
            if(paymentLog.getTaxAmt().compareTo(BigDecimal.ZERO)>0) {
                //核销还款  表外还款  需要处理税金
                String txnCode = "";
                //核销还款
                if(paymentLog.getTxnCode().startsWith("T96")){
                    if(TransBizConstant.ACCOUNT_TYPE_04.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_INTR_TAX;
                    }else if(TransBizConstant.ACCOUNT_TYPE_11.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_PEN_INTR_TAX;
                    }else if(TransBizConstant.ACCOUNT_TYPE_12.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CHARGEOFF_PAY_CODE_COMP_INTR_TAX;
                    }
                }else if(TransBizConstant.NON_ACCRUALS_FLA_1.equals(paymentLog.getNonAccrualsFlag())){
                    //表外还款
                    if(TransBizConstant.ACCOUNT_TYPE_04.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CODE_INTR_TAX_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_INTR_TAX_TRAN_OUT_INNON;
                        }
                    }else if(TransBizConstant.ACCOUNT_TYPE_11.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CODE_PENT_INTR_TAX_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_PENT_INTR_TAX_TRAN_OUT_INNON;
                        }
                    }else if(TransBizConstant.ACCOUNT_TYPE_12.equals(paymentLog.getConnectAccountType())){
                        txnCode = TransBizConstant.TXN_CODE_COMP_INTR_TAX_TRAN_INNON;
                        if("5".equals(paymentLog.getTxnCode().substring(4,5))){
                            txnCode =TransBizConstant.TXN_CODE_COMP_INTR_TAX_TRAN_OUT_INNON;
                        }
                    }
                }
                if(StringUtils.isNotEmpty(txnCode)){
                    TlGlInterfaceInvestorInfo taxGl = this.buildTransferGlInterfaceInfo(paymentLog,paymentLog.getTaxAmt(),txnCode);
                    if (null != taxGl) {
                        glInterfaceInfoList.add(taxGl);
                    }
                }

            }
        }
        return glInterfaceInfoList;
    }

    private TlGlInterfaceInvestorInfo buildTransferGlInterfaceInfo(TlPaymentAllocationInvestorLog paymentLog, BigDecimal glAmt, String txnCode) throws TxnException {
        if(glAmt.compareTo(BigDecimal.ZERO)<=0){
            return null;
        }

        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(txnCode,paymentLog.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }
        TlGlInterfaceInvestorInfo gl = new TlGlInterfaceInvestorInfo();
        try {
            gl.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(TxnRespCode.ERROR.getUserTips());
        }
        gl.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));
        gl.setInvestorId(paymentLog.getInvestorId());
        gl.setInvestorLoanId(paymentLog.getInvestorLoanId());
        gl.setGlDate(paymentLog.getPostingDate());
        gl.setBatchId(DateUtils.format(paymentLog.getPostingDate(), DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);
        gl.setTenantId(paymentLog.getTenantId());
        gl.setAmount(glAmt);
        gl.setLoanProductId(paymentLog.getProductId());
        gl.setTxnAccountId(paymentLog.getConnectAccountId());
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);
        gl.setOrganizationId(paymentLog.getOrganizationId());//　	机构号	　	同贷款订单表
        gl.setTxnCode(txnCode);//交易码
        gl.setLoanId(paymentLog.getLoanId());//贷款id
        gl.setCustomerId(paymentLog.getCustomerId());//客户号
        gl.setTxnAccountId(paymentLog.getAccountId());//交易账户号
        gl.setChannel(paymentLog.getChannel());
        gl.setLoanClassify(paymentLog.getLoanClassify());
        String txnDesc = transactionCodeTable.getChineseName();
        gl.setTxnDesc(txnDesc);
        gl.setCategory(paymentLog.getNonAccrualsFlag());
        gl.setTotalDays(paymentLog.getTotalTenor());
        gl.setTotalDays(paymentLog.getTotalDays());
        gl.setAccountingPhase(paymentLog.getAccountingPhase());
        gl.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        gl.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        gl.setLiabilityFlag(paymentLog.getLiabilityFlag());
        gl.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号
        gl.setUpdateBy(TransBizConstant.SYS_OPRATE);
        logger.info("通过09交易账户生成gl流水,gl:{}", BeanUtils.toMap(gl));
        return gl;
    }

    /**
     * 计提账户  生成税金计提gl
     *
     * @param businessDate 业务日期
     * @param amInterestAccrualInfo 累计账户
     * @param taxAmt 税金
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-26
     */
    @Override
    public TlGlInterfaceInvestorInfo generateGlInterfaceForAcccrualTax(Date businessDate, AmInterestAccrualInvestorInfo amInterestAccrualInfo, BigDecimal taxAmt) throws TxnException {
        if (taxAmt.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }

        String txnCode = amInterestAccrualInfo.getTxnCode();
        String tranCode = txnCode.substring(0,1)+(Integer.parseInt(txnCode.substring(1))+3);
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(tranCode,amInterestAccrualInfo.getTenantId());

        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }
        String txnDesc = transactionCodeTable.getChineseName();

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(amInterestAccrualInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(businessDate); //业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(businessDate, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(amInterestAccrualInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(tranCode);//交易码
        tlGlInterfaceInfo.setAmount(taxAmt);//交易金额
        tlGlInterfaceInfo.setLoanId(amInterestAccrualInfo.getLoanId());//贷款id
        tlGlInterfaceInfo.setLoanProductId(amInterestAccrualInfo.getProductId());//贷款产品id
        tlGlInterfaceInfo.setCustomerId(amInterestAccrualInfo.getCustomerId());//客户号
        tlGlInterfaceInfo.setTxnDesc(txnDesc);//交易描述
        tlGlInterfaceInfo.setTxnAccountId(amInterestAccrualInfo.getAccountId());//交易账户号
        tlGlInterfaceInfo.setLoanClassify(amInterestAccrualInfo.getLoanClassify());//五级分类
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(amInterestAccrualInfo.getChannel());//渠道	　	同贷款订单表
        tlGlInterfaceInfo.setCategory(amInterestAccrualInfo.getNonAccrualsFlag());
        tlGlInterfaceInfo.setTotalTenor(amInterestAccrualInfo.getTotalTenor());
        tlGlInterfaceInfo.setTotalDays(amInterestAccrualInfo.getTotalDays());
        tlGlInterfaceInfo.setAccountingPhase(amInterestAccrualInfo.getAccountingPhase());
        tlGlInterfaceInfo.setLiabilityFlag(amInterestAccrualInfo.getLiabilityFlag());
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorLoanId(amInterestAccrualInfo.getInvestorLoanId()); // 出资方贷款订单id
        tlGlInterfaceInfo.setInvestorId(amInterestAccrualInfo.getInvestorId()); // 出资方ID
        return tlGlInterfaceInfo;
    }

    /**
     * 计提账户  生成计提gl
     *
     * @param businessDate
     * @param amInterestAccrualInfo
     * @param taxAmt 税金
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-26
     */
    @Override
    public TlGlInterfaceInvestorInfo generateGlInterfaceForAcccrual(Date businessDate, AmInterestAccrualInvestorInfo amInterestAccrualInfo, BigDecimal taxAmt) throws TxnException {
        if (amInterestAccrualInfo.getAccrualDailyProvision().subtract(taxAmt).compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }

        String tranCode = amInterestAccrualInfo.getTxnCode();
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(tranCode,amInterestAccrualInfo.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }
        String txnDesc = transactionCodeTable.getChineseName();

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(amInterestAccrualInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(businessDate); //业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(businessDate, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(amInterestAccrualInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(tranCode);//交易码
        tlGlInterfaceInfo.setAmount(amInterestAccrualInfo.getAccrualDailyProvision().subtract(taxAmt));//交易金额
        tlGlInterfaceInfo.setLoanId(amInterestAccrualInfo.getLoanId());//贷款id
        tlGlInterfaceInfo.setLoanProductId(amInterestAccrualInfo.getProductId());//贷款产品id
        tlGlInterfaceInfo.setCustomerId(amInterestAccrualInfo.getCustomerId());//客户号
        tlGlInterfaceInfo.setTxnDesc(txnDesc);//交易描述
        tlGlInterfaceInfo.setTxnAccountId(amInterestAccrualInfo.getAccountId());//交易账户号
        tlGlInterfaceInfo.setLoanClassify(amInterestAccrualInfo.getLoanClassify());//五级分类
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(amInterestAccrualInfo.getChannel());//渠道	　	同贷款订单表
        tlGlInterfaceInfo.setCategory(amInterestAccrualInfo.getNonAccrualsFlag());
        tlGlInterfaceInfo.setTotalTenor(amInterestAccrualInfo.getTotalTenor());
        tlGlInterfaceInfo.setTotalDays(amInterestAccrualInfo.getTotalDays());
        tlGlInterfaceInfo.setAccountingPhase(amInterestAccrualInfo.getAccountingPhase());
        tlGlInterfaceInfo.setLiabilityFlag(amInterestAccrualInfo.getLiabilityFlag());
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorLoanId(amInterestAccrualInfo.getInvestorLoanId()); // 出资方订单id
        tlGlInterfaceInfo.setInvestorId(amInterestAccrualInfo.getInvestorId()); // 出资方id
        return tlGlInterfaceInfo;
    }

    /**
     * 累计账户生成成GL
     *
     * @param amInterestAccrualInfo 累计账户
     * @param glAmt 交易金额
     * @param txnCode 交易码
     * @param orgiStatus 会计状态
     * @param business 业务日期
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-27
     */
    @Override
    public TlGlInterfaceInvestorInfo buildTlGlInterfaceInvestorInfo(AmInterestAccrualInvestorInfo amInterestAccrualInfo, BigDecimal glAmt, String txnCode, String orgiStatus, Date business) throws TxnException {
        if (glAmt.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(txnCode,amInterestAccrualInfo.getTenantId());

        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(amInterestAccrualInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(business); //业务日期	YYYY-MM-DD	当前业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(business, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(amInterestAccrualInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(txnCode);//交易码 已有	消费信贷核算事件对应的交易码 具体值：按照消费信贷系统规范约定会计引擎模块会维护与消费信贷系统一致的数据字典
        tlGlInterfaceInfo.setAmount(glAmt);//交易金额	　	贷款订单表-贷款金额
        tlGlInterfaceInfo.setLoanId(amInterestAccrualInfo.getLoanId());//贷款id	　	同贷款订单表
        tlGlInterfaceInfo.setLoanProductId(amInterestAccrualInfo.getProductId());//　	贷款产品id	　	同贷款订单表
        tlGlInterfaceInfo.setCustomerId(amInterestAccrualInfo.getCustomerId());//	客户号	　	同贷款订单表
        String txnDesc = transactionCodeTable.getChineseName();
        tlGlInterfaceInfo.setTxnDesc(txnDesc);//交易描述	业务行摘要说明，源系统可视情况自动生成  产品名称
        tlGlInterfaceInfo.setTxnAccountId(amInterestAccrualInfo.getAccountId());//交易账户号	　	贷款订单表-客户收款账号
        tlGlInterfaceInfo.setLoanClassify(amInterestAccrualInfo.getLoanClassify());//五级分类	 同贷款订单表
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(amInterestAccrualInfo.getChannel());//渠道	　	同贷款订单表
        tlGlInterfaceInfo.setCategory(amInterestAccrualInfo.getNonAccrualsFlag());
        tlGlInterfaceInfo.setTotalTenor(amInterestAccrualInfo.getTotalTenor());
        tlGlInterfaceInfo.setTotalDays(amInterestAccrualInfo.getTotalDays());
        tlGlInterfaceInfo.setAccountingPhase(orgiStatus);
        tlGlInterfaceInfo.setLiabilityFlag(amInterestAccrualInfo.getLiabilityFlag());
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号	　	初始值：
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorId(amInterestAccrualInfo.getInvestorId());
        tlGlInterfaceInfo.setInvestorLoanId(amInterestAccrualInfo.getInvestorLoanId());
        return tlGlInterfaceInfo;
    }

    /**
     * 订单生成GL
     *
     * @param amLoanAccountInfo 订单账户信息
     * @param amLoanMainInfo 订单信息
     * @param txnCode 交易码
     * @param orgiStatus 会记状态
     * @param bussinessDate 业务日期
     * @param prinNonAccrualFlag 本金转非应计标志  0不转列  1转列
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-27
     */
    @Override
    public TlGlInterfaceInvestorInfo buildTlGlInterfaceInvestorInfo(AmLoanAccountInvestorInfo amLoanAccountInfo, AmLoanMainInvestorInfo amLoanMainInfo, String txnCode, String orgiStatus, Date bussinessDate, String prinNonAccrualFlag) throws TxnException {
        BigDecimal glAmt = amLoanAccountInfo.getRemainPrincipal().subtract(amLoanAccountInfo.getPrincipalBalance());
        if (glAmt.compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(txnCode,amLoanAccountInfo.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(amLoanAccountInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(bussinessDate); //业务日期	YYYY-MM-DD	当前业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(bussinessDate, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(amLoanAccountInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(txnCode);//交易码 已有	消费信贷核算事件对应的交易码 具体值：按照消费信贷系统规范约定会计引擎模块会维护与消费信贷系统一致的数据字典
        tlGlInterfaceInfo.setAmount(glAmt);//交易金额	　	贷款订单表-贷款金额
        tlGlInterfaceInfo.setLoanId(amLoanAccountInfo.getLoanId());//贷款id	　	同贷款订单表
        tlGlInterfaceInfo.setLoanProductId(amLoanAccountInfo.getProductId());//　	贷款产品id	　	同贷款订单表
        tlGlInterfaceInfo.setCustomerId(amLoanAccountInfo.getCustomerId());//	客户号	　	同贷款订单表
        String txnDesc = transactionCodeTable.getChineseName();
        tlGlInterfaceInfo.setTxnDesc(txnDesc);//交易描述	业务行摘要说明，源系统可视情况自动生成  产品名称
        tlGlInterfaceInfo.setTxnAccountId(amLoanAccountInfo.getLoanId());//交易账户号	　	贷款订单表-客户收款账号
        tlGlInterfaceInfo.setLoanClassify(amLoanAccountInfo.getLoanClassify());//五级分类	 同贷款订单表
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(amLoanAccountInfo.getChannel());//渠道	　	同贷款订单表
        String category = TransBizConstant.NON_ACCRUALS_FLA_0;
        if(TransBizConstant.TRANSFER_FLAG_1.equals(prinNonAccrualFlag)){
            //本金转非应计   非应计类别取订单账户    否则为0应计
            category=amLoanAccountInfo.getNonAccrualsFlag();
        }
        tlGlInterfaceInfo.setCategory(category);
        tlGlInterfaceInfo.setTotalTenor(amLoanMainInfo.getTotalTenor());
        tlGlInterfaceInfo.setTotalDays(amLoanMainInfo.getTotalDays());
        tlGlInterfaceInfo.setAccountingPhase(orgiStatus);
        tlGlInterfaceInfo.setLiabilityFlag(TransBizConstant.LIABILITY_FLAG_00);
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号	　	初始值：
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorId(amLoanMainInfo.getInvestorId());
        tlGlInterfaceInfo.setInvestorLoanId(amLoanMainInfo.getInvestorLoanId());
        return tlGlInterfaceInfo;
    }

    /**
     * 贷款时总账流水生成
     *
     * @param amLoanMainInfo    订单信息
     * @param amLoanAccountInfo 订单账户账户信息
     * @param productName       产品名称
     * @param business          业务日期
     * @param liabilityFlag     债务归属
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-05-28
     */
    @Override
    public TlGlInterfaceInvestorInfo buildTlGlInterfaceInvestorInfo(AmLoanMainInvestorInfo amLoanMainInfo, AmLoanAccountInvestorInfo amLoanAccountInfo, String productName, Date business, String liabilityFlag) throws TxnException {

        if (amLoanMainInfo.getLoanAmount().compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(TransBizConstant.TXN_CODE_INSTSUM,amLoanMainInfo.getTenantId());

        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }

        TlGlInterfaceInvestorInfo tlGlInterfaceInfo = new TlGlInterfaceInvestorInfo();
        try {
            tlGlInterfaceInfo.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        tlGlInterfaceInfo.setTenantId(amLoanAccountInfo.getTenantId());
        tlGlInterfaceInfo.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));//流水号 	调用统一的工具类得到全局流水号
        tlGlInterfaceInfo.setGlDate(business); //业务日期	YYYY-MM-DD	当前业务日期
        tlGlInterfaceInfo.setBatchId(DateUtils.format(business, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);//批次号
        tlGlInterfaceInfo.setLiabilityFlag(liabilityFlag);
        // 业务日期 +01
        // 源系统可以按照一定逻辑生成（比如每次生成一个数据文件，同时生成一个批次号），目前一阶段每日仅有一个批次文件，规则：YYYYMMDD＋01
        // 	YYYYMMDD＋01
        tlGlInterfaceInfo.setOrganizationId(amLoanMainInfo.getOrganizationId());//　	机构号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnCode(TransBizConstant.TXN_CODE_INSTSUM);//交易码 已有	消费信贷核算事件对应的交易码 具体值：按照消费信贷系统规范约定会计引擎模块会维护与消费信贷系统一致的数据字典
        tlGlInterfaceInfo.setAmount(amLoanMainInfo.getLoanAmount());//交易金额	　	贷款订单表-贷款金额
        tlGlInterfaceInfo.setLoanId(amLoanMainInfo.getLoanId());//贷款id	　	同贷款订单表
        tlGlInterfaceInfo.setLoanProductId(amLoanMainInfo.getProductId());//　	贷款产品id	　	同贷款订单表
        tlGlInterfaceInfo.setCustomerId(amLoanMainInfo.getCustomerId());//	客户号	　	同贷款订单表
        tlGlInterfaceInfo.setTxnDesc(productName);//交易描述	业务行摘要说明，源系统可视情况自动生成  产品名称
        tlGlInterfaceInfo.setTxnAccountId(amLoanMainInfo.getLoanId());//交易账户号	　	贷款订单表-客户收款账号 TODO 放款配置改为loanId
        tlGlInterfaceInfo.setLoanClassify(amLoanAccountInfo.getLoanClassify());//五级分类	 同贷款订单表
        tlGlInterfaceInfo.setImpairFlag(TransBizConstant.NO_FLAG);//贷款是否发生单笔减值	0 = 否   1 = 是 	初始固定值：0
        tlGlInterfaceInfo.setGlSource(TransBizConstant.GL_INTERFACE_SOURCE);//数据来源	固定值CLS	固定值 CLS
        tlGlInterfaceInfo.setChannel(amLoanMainInfo.getChannel());//渠道	　	同贷款订单表
        tlGlInterfaceInfo.setCategory(amLoanAccountInfo.getNonAccrualsFlag());
        tlGlInterfaceInfo.setTotalTenor(amLoanMainInfo.getTotalTenor());
        if(null != amLoanMainInfo.getTotalDays()) {
            tlGlInterfaceInfo.setTotalDays(amLoanMainInfo.getTotalDays());
        }
        tlGlInterfaceInfo.setAccountingPhase(amLoanAccountInfo.getAccountingPhase());
        tlGlInterfaceInfo.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);//	记录版本号	　	初始值：
        tlGlInterfaceInfo.setUpdateBy(TransBizConstant.SYS_OPRATE);
        tlGlInterfaceInfo.setInvestorId(amLoanMainInfo.getInvestorId());
        tlGlInterfaceInfo.setInvestorLoanId(amLoanMainInfo.getInvestorLoanId());
        return tlGlInterfaceInfo;
    }

    /**
     * 生成取消订单GL
     *
     * @param orgiGl 原GL流水
     * @param businessDate 业务日期
     * @return TlGlInterfaceInvestorInfo GL流水
     * @throws TxnException
     * @author liuyang
     * @date 2020-06-15
     */
    @Override
    public TlGlInterfaceInvestorInfo buildTlGlInterfaceInvestorInfo(TlGlInterfaceInvestorInfo orgiGl, Date businessDate) throws TxnException {
        String  cancleTxnCode = this.getCancleTxnCode(orgiGl.getTxnCode());
        PrTransactionCodeTable transactionCodeTable = transactionCodeTableService.findByCodeAndTenantId(cancleTxnCode,orgiGl.getTenantId());
        if(TransBizConstant.GENERAL_LEDGER_FLAG_0.equals(transactionCodeTable.getGeneralLedgerFlag())){
            return null;
        }

        TlGlInterfaceInvestorInfo saveGl = new TlGlInterfaceInvestorInfo();
        BeanMapping.copy(orgiGl,saveGl);
        try {
            saveGl.setId(segmentService.getId(TransBizConstant.TL_GL_INTERFACE_INVESTOR_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        saveGl.setBatchId(DateUtils.format(businessDate, DateUtils.DATE_PATTERN_ANOTHER) + TransBizConstant.BATCH_NUMBER);
        saveGl.setGlFlowno(SeqUtils.getSNByLength(TransBizConstant.GL_FLOW_NO_RANDOM));
        if(cancleTxnCode.equals(orgiGl.getTxnCode())){
            saveGl.setAmount(orgiGl.getAmount().negate());
        }else {
            saveGl.setAmount(orgiGl.getAmount());
        }
        saveGl.setTxnCode(cancleTxnCode);
        saveGl.setTxnDesc(transactionCodeTable.getChineseName());
        saveGl.setGlDate(businessDate);
        saveGl.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);
        return saveGl;
    }

    private String getCancleTxnCode(String txnCode) {
        switch (txnCode){
            case TransBizConstant.TXN_CODE_INSTSUM :
                return TransBizConstant.TXN_CODE_INSTSUM_REVOCATION;
            case TransBizConstant.TXN_CODE_INST_INTR_ACCU :
                return  TransBizConstant.TXN_CODE_INTR_REVOCATION;
            case TransBizConstant.TXN_CODE_PEN_INTR_ACCU :
                return  TransBizConstant.TXN_CODE_PENT_INTR_REVOCATION;
            case TransBizConstant.TXN_CODE_COM_INTR_ACCU :
                return  TransBizConstant.TXN_CODE_COMP_INTR_REVOCATION;
            case TransBizConstant.TXN_CODE_INST_INTR_TAX_ACCU :
                return  TransBizConstant.TXN_CODE_INTR_TAX_REVOCATION;
            case TransBizConstant.TXN_CODE_PEN_INTR_TAX_ACCU :
                return  TransBizConstant.TXN_CODE_PENT_INTR_TAX_REVOCATION;
            case TransBizConstant.TXN_CODE_COM_INTR_TAX_ACCU :
                return  TransBizConstant.TXN_CODE_COMP_INTR_TAX_REVOCATION;
            case TransBizConstant.TXN_CODE_INST_WAIVE_INTR_ACCU :
                return TransBizConstant.TXN_CODE_WAIVE_INTR_REVOCATION;
            case TransBizConstant.TXN_CODE_INST_WAIVE_INTR_TAX_ACCU :
                return  TransBizConstant.TXN_CODE_WAIVE_INTR_TAX_REVOCATION;
            default :
                return txnCode;
        }
    }
}
